package spring.in.action.chapter_03.conditional;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class MagicExistsCondition implements Condition
{

    @Override
    public boolean matches(ConditionContext pContext, AnnotatedTypeMetadata pMetadata)
    {
        BeanDefinitionRegistry lRegistry = pContext.getRegistry();// 可以检查bean的定义
        ConfigurableListableBeanFactory lBeanFactory = pContext.getBeanFactory();// 检查bean是否存在，及bean的属性
        ResourceLoader lResourceLoader = pContext.getResourceLoader();// 读取并探查ResourceLoader所加载的资源
        ClassLoader lClassLoader = pContext.getClassLoader();// 加载并检查类是否存在
        Environment lEnvironment = pContext.getEnvironment();// 检查环境变量是否存在以及它的值是什么

        System.out.println("Condition.matches");
        return lEnvironment.containsProperty("magic");
    }

}
